<?php

// +---------------------------------------------+
// |     Copyright � 2006 � Rene Richter         |
// |     http://www.wiesental-racing.de          |
// |     This file may not be redistributed.     |
// +---------------------------------------------+


function rFQImageLine($imageurl)
{
	return "<tr><td colspan='2' align='center'><img src='$imageurl' class='bbcode' alt='' style='vertical-align:middle; border:0'/></td></tr>";
}


function rFQInfoLine($caption, $value)
{	
	return "<tr><td style='text-align:right'><b>$caption:</b></td><td style='text-align:left'>$value</td></tr>";
}

function rfQOneLine($text)
{
	return "<tr><td colspan='2' style='text-align:center'><center>$text</center></td></tr>";
}


class rFQTable
{
	var $content;
	
	function rFQTable($width)
	{
		$this->content = '';
		
		if (isset($width) && (strlen($width) > 0))
		{
			$this->content = "<table width='$width'>";	
		}
		else
		{
			$this->content = '<table>';
		}
	}

	function HdrCenter($value, $class='')
	{
		$this->ColCenter($value, $class . ' tTitle');
	}
	
	function HdrLeft($value, $class='')
	{
		$this->ColLeft($value, $class . ' tTitle');
	}
	
	function HdrRight($value, $class='')
	{
		$this->ColRight($value, $class . ' tTitle');
	}
	
	function BeginRow()
	{
		$this->content .= '<tr>';
	}
	
	function EndRow()
	{
		$this->content .= '</tr>';
	}
	
	function ColCenter($value, $class='')
	{
	    $class == '' ?
		     $this->content .= "<td style='text-align:center'>$value</td>" :
			 $this->content .= "<td class='" . $class . "' style='text-align:center'>$value</td>" ;
	}	
		
	function ColLeft($value, $class='')
	{
	    $class == '' ?
		     $this->content .= "<td style='text-align:left'>$value</td>" :
			 $this->content .= "<td class='" . $class . "' style='text-align:left'>$value</td>" ;
	}
	
	function ColRight($value, $class='')
	{
	    $class == '' ?
		     $this->content .= "<td style='text-align:right'>$value</td>" :
			 $this->content .= "<td class='" . $class . "' style='text-align:right'>$value</td>" ;
	}

	
	function GetContent()
	{
		return $this->content . '</table>';
	}	
}




class rFQDisplayHelper
{
	var $motd;
	var $series;
	var $track;
	var $drivers;
	var $session_info;
        var $servername;
	
	var $fullsrvaddr;
	var $serverURL;
	
	var $session_names = array();
	

	function rFQDisplayHelper($rFq, $session_names, $lang)
	{
		$this->session_names = $session_names;		

		// The server is online so lets display the information
                $this->servername = htmlentities($rFq->_getServerName());
		$this->motd    = htmlentities($rFq->_getServerMOTD());
		$this->series  = htmlentities($rFq->_getSeries());
		$this->track   = htmlentities($rFq->_getTrack());
		$this->drivers = $rFq->_getCurPlayers() . ' / ' . $rFq->_getMaxPlayers();
		
		$this->fullsrvaddr = $rFq->_getFullServerAddr();
		$this->serverURL   = $rFq->_getServerURL();
		
		$this->session_info = "-";
		
		$session = $rFq->_getSession();
		if (array_key_exists($session, $this->session_names))
		{
			$this->session_info = htmlentities($this->session_names[$session]);
			
			$remaining = '';
			if ($rFq->_getSessionTime() > 0)
			{
				//$this->session_info .= " [" . gmdate("H:i:s", $rFq->_getSessionTime()) . "]";	
				$remaining = gmdate("H:i:s", $rFq->_getSessionTime());
			}
			
			if ($rFq->_getSessionLaps() < 1024)
			{
				if (strlen($remaining) > 0) $remaining .= ' / ';
				$laps_text = ($rFq->_getSessionLaps() != 1) ? $lang['server_laps'] : $lang['server_lap'];
				$remaining .= $rFq->_getSessionLaps() . " $laps_text";				
			}
			
			if (strlen($remaining) > 0)
			{
				$this->session_info .= " [$remaining]";	
			}
		}		
	}
}


function DivideAndSub(&$value, $div)
{
	$res = floor($value  / $div);
	$value -= $res * $div;
	return $res;
}


function SecondsToString($value)
{
	$res = '';
	
	if (isset($value))
	{
		$value = round($value, 3);
		
		$hours = DivideAndSub($value, 3600);
		$minutes = DivideAndSub($value, 60);
		$seconds = DivideAndSub($value, 1);
		$milliseconds = $value * 1000;

		if (0 < $hours)	
		{
			$res = sprintf("%d:%02d:%02d.%03d", $hours, $minutes, $seconds, $milliseconds);
		}
		else if (0 < $minutes)
		{
			$res = sprintf("%d:%02d.%03d", $minutes, $seconds, $milliseconds);
		}
		else
		{
			$res = sprintf("%d.%03d", $seconds, $milliseconds);
		}

	}
	
	return $res;
}


?>